﻿
using System.Collections.ObjectModel;
using System.Web.Profile;
using System.Web.Security;
using FinalSilverlightProject.Web.Models;
using FinalSilverlightProject.Web.Resources;

namespace FinalSilverlightProject.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;


    // TODO: Create methods containing your application logic.
    [EnableClientAccess]
    [RequiresAuthentication]
    [AdministratorAndCustomerRole]
    public class EditUserAcountService : DomainService
    {
        [Invoke(HasSideEffects = true)]
        public EditRegistrationData GetUserRegistrationInfo(string userName)
        {
            if (userName != null)
            {
                var user = Membership.GetUser(userName);
                var profile = ProfileBase.Create(userName, true);
                if (user != null)
                {
                    var result = new EditRegistrationData()
                                                  {
                                                      UserName = user.UserName,
                                                      Email = user.Email,
                                                      City = profile.GetPropertyValue("City").ToString(),
                                                      Country = profile.GetPropertyValue("Country").ToString(),
                                                      FirstName = profile.GetPropertyValue("FirstName").ToString(),
                                                      FriendlyName = profile.GetPropertyValue("FriendlyName").ToString(),
                                                      LastName = profile.GetPropertyValue("LastName").ToString(),
                                                      PostCode = profile.GetPropertyValue("PostCode").ToString(),
                                                      StreetAddress = profile.GetPropertyValue("StreetAddress").ToString(),
                                                      TelephoneNumber = profile.GetPropertyValue("TelephoneNumber").ToString()
                                                  };
                    return result;
                }
            }
            return null;
        }

        [Invoke(HasSideEffects = true)]
        public void UpdateUserRegistrationInfo(EditRegistrationData userRegistrationData)
        {
            if (userRegistrationData == null || userRegistrationData.UserName == null) return;
            var user = Membership.GetUser(userRegistrationData.UserName);
            var profile = ProfileBase.Create(userRegistrationData.UserName, true);
            if (user != null)
            {
                user.Email = userRegistrationData.Email;
                //if (!String.IsNullOrWhiteSpace(oldPassword) && !String.IsNullOrWhiteSpace(newPassword))
                //    user.ChangePassword(oldPassword, newPassword);
                Membership.UpdateUser(user);

                profile.SetPropertyValue("City", userRegistrationData.City);
                profile.SetPropertyValue("Country", userRegistrationData.Country);
                profile.SetPropertyValue("FirstName", userRegistrationData.FirstName);
                profile.SetPropertyValue("FriendlyName", userRegistrationData.FriendlyName);
                profile.SetPropertyValue("LastName", userRegistrationData.LastName);
                profile.SetPropertyValue("PostCode", userRegistrationData.PostCode);
                profile.SetPropertyValue("StreetAddress", userRegistrationData.StreetAddress);
                profile.SetPropertyValue("TelephoneNumber", userRegistrationData.TelephoneNumber);
                profile.Save();
            }
        }

    }
}


